package com.feature50.clarity.formui.jfd;

import com.feature50.clarity.formui.FormUI;
import com.feature50.clarity.FormException;
import com.jformdesigner.runtime.FormCreator;
import com.jformdesigner.runtime.NoSuchComponentException;

import javax.swing.JComponent;
import java.lang.ref.WeakReference;

public class FormUIJFormDesigner extends FormUI {
    private JComponent root;
    private FormCreator creator;

    public FormUIJFormDesigner(FormCreator creator) {
        this.creator = creator;
    }

    public JComponent getRootComponent() {
        if (root == null) {
            try {
                root = (JComponent) creator.create();
            } catch (Exception e) {
                throw new FormException("Couldn't create root component", e);
            }
        }

        return root;
    }

    public JComponent getComponent(String name) {
        try {
            return (JComponent) creator.getComponent(name);
        } catch (NoSuchComponentException e) {
            return null;
        }
    }
}